package com.ttl.bc.ic.stktransfer2;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ttl.common.util.CommonUtils;

public class BCStkTransfer2Service {

	@SuppressWarnings("unchecked")
	public BCStkTransfer2 getBCStkTransfer2(String shelfCode, String dateStr) {
		String yearStr = "";
		String monthStr = "";
		
		Date date = new Date();
		if (CommonUtils.isValidDate(dateStr)) {
			date = CommonUtils.convertStringToDate(dateStr);			
		} 
		SimpleDateFormat simpleYear = new SimpleDateFormat("yyyy");
		yearStr = simpleYear.format(date);
		
		if(Integer.valueOf(yearStr)<2500) {
			int yearStr2 = Integer.valueOf(yearStr)+543;
			yearStr = (""+yearStr2).substring(2);
		} else {
			yearStr = yearStr.substring(2);
		}
		
		SimpleDateFormat simpleMonth = new SimpleDateFormat("MM");
		monthStr = simpleMonth.format(date);		
		
		String docNoStr = "";
		if (shelfCode.equals("10")) {
			docNoStr = "TF-CS-";
		} else if (shelfCode.equals("12")) {
			docNoStr = "TF-PD-";
		} else if (shelfCode.equals("16")) {
			docNoStr = "TF-YD-";
		} else if (shelfCode.equals("17")) {
			docNoStr = "TF-FF-";
		}
		docNoStr +=yearStr+monthStr;
		
		BCStkTransfer2 s = new BCStkTransfer2();
		String hql = "SELECT MAX(docNo) FROM BCStkTransfer2 WHERE docNo like '"+docNoStr+"%' ";

		BCStkTransfer2DAO dao = new BCStkTransfer2DAO();				
		
		List result = dao.find(hql);
		String docNo = null;
		
		String maxDocNo = null;
		if(result.get(0) != null) {
			
			docNo = result.get(0).toString();
			int temp = 1+Integer.valueOf(docNo.substring(docNo.length()-3,docNo.length()));			
			NumberFormat formatter = new DecimalFormat("000");
			docNo = formatter.format(temp);
			
			maxDocNo = docNoStr+"-"+docNo; 			
		}else {
			maxDocNo = docNoStr+"-001";
		}
		//System.out.println("maxDocno = "+maxDocNo);			
		s.setDocNo(maxDocNo);
			
		return s; 
	}
}
